package cn.guetzjb.controller;

import cn.guetzjb.core.constant.CacheConstants;
import cn.guetzjb.core.entity.R;
import cn.guetzjb.redis.service.RedisService;
import cn.guetzjb.security.utils.SecurityUtils;
import cn.hutool.captcha.*;
import cn.hutool.core.lang.UUID;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController()
@RequestMapping("/captcha")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "验证码")
public class CaptchaController {

    private final RedisService redisService;

    @Operation(summary = "获取验证码")
    @GetMapping("/image")
    public R getCaptchaImage() {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100);
        log.info("验证码为：{}", circleCaptcha.getCode());
        String uuidKey = UUID.randomUUID().toString();
        redisService.setCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuidKey, circleCaptcha.getCode(), CacheConstants.CAPTCHA_TIME, TimeUnit.SECONDS);
        return R.ok(Map.of("image", circleCaptcha.getImageBase64(), "uuidKey", uuidKey));
    }
}
